﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;

namespace Xid.Controls.SampleCompiler
{
    public partial class ForeignKeyMapCtrl : UserControl
    {
        private ForeignKeyMap _fKeyMap;

        private ForeignKeyMap _fkmProto;

        public ForeignKeyMap KeyMapProtoType
        {
            get { return _fkmProto; }
            set { _fkmProto = value; }
        }


        public string KeyMap
        {
            get { return _fKeyMap; }// == null ? "" : (string)_fKeyMap; }
            set
            {
                if (_fKeyMap != null)
                    _fKeyMap.CollectionChanged -= new EventHandler(_fKeyMap_CollectionChanged);
                _fKeyMap = (ForeignKeyMap)value;
                if (_fKeyMap != null && _fKeyMap.Count > 0)
                    _fKeyMap.CollectionChanged += new EventHandler(_fKeyMap_CollectionChanged);
                else if (_fkmProto != null)
                    KeyMap = _fkmProto;



                Init();
            }
        }

        void _fKeyMap_CollectionChanged(object sender, EventArgs e)
        {
            OnKeyMapChanged();
        }

        private void Init()
        {
            if (_fkmRebuildNeeded)
            {
                tableLayoutPanel1.Controls.Clear();
                if (_fKeyMap != null)
                {
                    tableLayoutPanel1.SuspendLayout();
                    tableLayoutPanel1.RowCount = _fKeyMap.Count;
                    int currentRow = 0;
                    foreach (var item in _fKeyMap)
                    {
                        Label l = new Label();
                        l.Text = item.Key;
                        l.Name = item.Key + "_label";
                        l.Anchor = AnchorStyles.Left;
                        l.AutoSize = true;
                        tableLayoutPanel1.Controls.Add(l, 0, currentRow);
                        l.Margin = new Padding(1);

                        ComboBox cb = new ComboBox();
                        cb.Name = item.Key;
                        cb.Text = item.Value;
                        cb.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                        cb.Margin = new Padding(0);
                        cb.DataBindings.Add("Text", item, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
                        //cb.Items.Add("A");
                        //cb.Items.Add("B");
                        //cb.Items.Add("C");
                        cb.TextChanged += new EventHandler(cb_TextChanged);
                        cb.LostFocus += new EventHandler(cb_LostFocus);
                        cb.GotFocus += new EventHandler(cb_GotFocus);
                        tableLayoutPanel1.Controls.Add(cb, 1, currentRow);


                        if (tableLayoutPanel1.RowStyles.Count < currentRow + 1)
                            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                        else
                            tableLayoutPanel1.RowStyles[currentRow].SizeType = SizeType.AutoSize;
                        currentRow++;

                    }
                    tableLayoutPanel1.ResumeLayout(true);

                    this.Height = tableLayoutPanel1.PreferredSize.Height;
                    this.Visible = true;

                }
                else
                    this.Visible = false;
            }
        }


        private bool fileteringPending = false;
        void cb_GotFocus(object sender, EventArgs e)
        {
            try
            {
                if (!fileteringPending)
                {
                    fileteringPending = true;
                    ComboBox cb = sender as ComboBox;
                    if (_dataProvider != null)
                    {
                        cb.Items.Clear();

                        StringBuilder sb = new StringBuilder();

                        Dictionary<string, string> filter = new Dictionary<string, string>();
                        foreach (Control item in tableLayoutPanel1.Controls)
                        {
                            if (item is ComboBox && item != cb)
                            {
                                sb.AppendFormat("{0}={1};", item.Name, item.Text);
                                filter.Add(item.Name, item.Text);
                            }

                        }
                        if (sb.Length > 0)
                            sb.Remove(sb.Length - 1, 1);

                        foreach (var item in _dataProvider.Provider.FilterKeyValues(cb.Name, filter))
                        {
                            cb.Items.Add(item);
                        }
                    }
                    fileteringPending = false;
                }
            }
            catch (Exception ex)
            {
                Microsoft.SqlServer.MessageBox.ExceptionMessageBox box = new Microsoft.SqlServer.MessageBox.ExceptionMessageBox(ex);
                box.Show(this);
            }
        }


        DataProviderResource _dataProvider;
        bool _fkmRebuildNeeded = true;
        public void Setup(DataProviderResource dpr)
        {
            if (_dataProvider != dpr)
            {
                _fkmRebuildNeeded = true;
                _dataProvider = dpr;
            }
        }

        void cb_TextChanged(object sender, EventArgs e)
        {
            ComboBox c = sender as ComboBox;
            _fKeyMap[c.Name] = c.Text;
            //valueChanged = true;
            //this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        }

        void cb_LostFocus(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        public ForeignKeyMapCtrl()
        {
            InitializeComponent();
        }


        public event EventHandler KeyMapChanged;
        protected virtual void OnKeyMapChanged()
        {
            if (KeyMapChanged != null)
                KeyMapChanged(this, new EventArgs());
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}
